/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.shell.handler;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.hyts.shell.config.SSHConfig;
import com.hyts.shell.error.SSHException;

/** 
 * @title BaseProcessStream.java  
 * @package com.hyts.shell.handler  
 * @description 基础操作流处理器  
 * @see TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月17日 
 * @version V1.0 
 */
public class BaseProcessStream implements AbstractStreamHandler<String> {

	/* (非 Javadoc)
	 * @param is
	 * @param charsetName
	 * @return
	 * @see com.hyts.shell.handler.AbstractStreamHandler#processStream(java.io.InputStream, java.lang.String)  
	 * @exception
	 */ 
	@Override
	public String processStream(InputStream is, String charsetName) {
		charsetName = (charsetName == null || charsetName.isEmpty())?SSHConfig.DEFAULT_SSH_CHARSET:charsetName;
		StringBuilder content = new StringBuilder();
		try(
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,charsetName));
			){
			reader.lines().forEach(param->{
				content.append(param);
			});
			return content.toString();
		}catch(IOException e) {
			 throw new SSHException("处理返回结果流时，出现错误!",e);
		}
	}
}
